DIP-Introductory python tutorials for image processing(1-21)-Python Basics

学习自 Youtube 博主 DigitalSreeni。

相关资源

课程

Tutorial 01 - Why is it important for researchers to learn coding?

Tutorial 02 - What is digital image processing?

Tutorial 03 - Image processing in imageJ, ZEN, and APEER

Tutorial 04 - Appreciating the simplicity of python

  • 使用 Python 给图像添加 Sigma=2 的高斯模糊
python
from skimage import io, filters
from matplotlib import pyplot as plt
 
img = io.imread('images/Osteosarcoma_01_25Sigma_noise.tif')  # 读取图片
gaussian_img = filters.gaussian(img, sigma=2)  # 添加高斯模糊
 
plt.imshow(gaussian_img)  # 显示图片
png

Tutorial 05 - How to install python using Anaconda

教你怎么装 Anaconda 和 Spyder..

Tutorial 06 - Understanding Anaconda Packages

教你怎么用 Anaconda ..

Tutorial 06b - Understanding python environments (using Anaconda)

教你怎么管理 Anaconda 环境..

Tutorial 07 - Getting familiar with spyder IDE

教你怎么用 Sypder..

png

Variable explorer 可以实时显示变量的类型和值?有点意思

Tutorial 08 - What are libraries in python

介绍了 Python 中库(library)的概念

Tutorial 09 - Top 5 python libraries for image analysis

PyPI · The Python Package Index

  • Image processing
    • scikit-image
    • Opencv-python
  • Data analysis
    • numpy
    • pandas
  • Plotting & visualization
    • matplotlib
  • Other libraries worth mentioning
    • scipy - scientfic and numerical tools, extension of numpy
    • keras / tensorflow - deep learning
    • seaborn and plotly - advanced, highly customizable plotting
    • czifile, tifffile - many other libraries for specific tasks

Tutorial 10 - Writing your first lines of code in Python

教你怎么在 Spyder 里写 Python 代码, 算了, 我还是用 Jupyter 吧..

Tutorial 11 - Operators and basic math in Python

教你怎么用 Python 里的运算符和基本运算..

Tutorial 12 - What are Lists in Python

教你怎么用 Python 里的 list..

Tutorial 13 - What are Tuples in Python

教你怎么用 Python 里的 tuple..

就是个定义以后值不可变的 list..

Tutorial 14 - What are Dictionaries in Python

教你怎么用 Python 里的字典..

Dictionaries: a key and a value

  • 定义字典的 3 种方式:
python
life_sciences = {'Botany': 'plants', 
                 'Zoology': 'animals',
                 'Virology': 'viruses',
                 'Cell_biology': 'cells'}
python
life_sciences = dict([('Botany', 'plants'),
                      ('Zoology', 'animals'),
                      ('Virology', 'viruses'),
                      ('Cell_biology', 'cells')])
python
life_sciences = dict(Botany = 'plants',
                     Zoology = 'animals',
                     Virology = 'viruses',
                     Cell_biology='cells')
  • 判断定义变量的类型
python
type(life_sciences)
dict
  • 查找
python
print('Zoology' in life_sciences)
True
  • 添加元素
python
life_sciences['Neuroscience'] = 'nervous_system'
python
print(life_sciences)
{'Botany': 'plants', 'Zoology': 'animals', 'Virology': 'viruses', 'Cell_biology': 'cells', 'Neuroscience': 'nervous_system'}
  • 删除元素
python
del life_sciences['Neuroscience']
python
print(life_sciences)
{'Botany': 'plants', 'Zoology': 'animals', 'Virology': 'viruses', 'Cell_biology': 'cells'}
  • 定义字典时只能用 tuple 作 key 而不是 list:
python
b = {(1, 0): 'a', (1, 1): 'b', (2, 2): 'c', (3, 2): 'd'}
python
c = {[1, 0]: 'a', [1, 1]: 'b', [2, 2]: 'c', [3, 2]: 'd'}
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Input In [12], in <cell line: 1>()
----> 1 c = {[1, 0]: 'a', [1, 1]: 'b', [2, 2]: 'c', [3, 2]: 'd'}


TypeError: unhashable type: 'list'
  • 输出字典的 key
python
d = list(life_sciences.keys())
d
['Botany', 'Zoology', 'Virology', 'Cell_biology']
  • 输出字典的 value
python
e = list(life_sciences.values())
e
['plants', 'animals', 'viruses', 'cells']

Tutorial 15 - What are numpy arrays in Python

教你了解 numpy

  • numpy 与 list 的不同:
python
a = [1, 2, 3, 4, 5]
b = 2 * a
b
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
python
type(a)
list
python
import numpy as np
 
c = np.array(a)
d = 2 * c
d
array([ 2,  4,  6,  8, 10])
python
type(c)
numpy.ndarray
python
c ** 2
array([ 1,  4,  9, 16, 25], dtype=int32)
  • 设置数据类型
python
import numpy as np
 
x = np.array([[1, 2], [3, 4]])  # 整型变量
y = np.array([[1, 2], [3, 4]], dtype=np.float64)  # float
y/2
array([[0.5, 1. ],
       [1.5, 2. ]])
  • 读取图像, 将图像存储为 numpy 矩阵
python
from skimage import io
 
img1 = io.imread('images/Osteosarcoma_01.tif')
type(img1)
numpy.ndarray
  • 图像(高, 宽, 通道数)
python
img1.shape
(1104, 1376, 3)
  • 创建图像
    • 创建一个值全为 1, 大小和 img1 相同的图像
python
a = np.ones_like(img1)
a.shape
(1104, 1376, 3)
  • 切片
python
import numpy as np
 
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
np.shape(a)
(3, 4)
python
a
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
python
a[:2]
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
python
a[:2, 1:3]
array([[2, 3],
       [6, 7]])
  • 求和
python
np.sum(a, axis=0)
array([15, 18, 21, 24])
python
np.sum(a, axis=1)
array([10, 26, 42])
python
np.max(a)
12

Tutorial 16 - Data types in python

  • Data types:
类型名称
Textstr
Numbersint, float, complex
Arrays(lists)list, tuple, range
Mappingdict
Boolean(True/False)bool
  • For image processing using skimage:
变量名值域
uint80 to 255
uint160 to 65535
uint320 to 2^32-1
float-1 to 1 or 0 to 1
int8-128 to 127
int16-32768 to 32767
int32-2^32 to 2^32-1
  • 实例
python
from skimage import io, img_as_float
 
img = io.imread('images/Osteosarcoma_01.tif')
img2 = img_as_float(img)
png
python
img.max()
255
python
img2.max()
1.0

Tutorial 17 - if and else statements in python

教你怎么用 Python 里的 if-else..

Tutorial 18 - while loops in python

教你怎么用 Python 里的 while 循环..

Tutorial 19 - for loops in python

教你怎么用 Python 里的 for 循环..

通常用于遍历数组里的值

Tutorial 20 - Functions in Python

教你怎么用 Python 里的函数 ..

Tutorial 21 - Lambda Functions in Python

  • 普通函数
python
def squared(x):
    return x ** 2
python
squared(4)
16
  • lambda 函数
python
a = lambda x: x ** 2
a(5)
25
  • 使用 lambda 函数
python
a = lambda x, y: 2 * x ** 2 + 3 * y
a(3, 5)
33
  • lambda 函数和普通函数混合使用
python
# S = ut + 1 / 2 a * t ** 2
 
def distance_eqn(u, a):
    return lambda t: u * t + ((1 / 2) * a * t ** 2)
python
dist = distance_eqn(5, 10)
dist(20)
2100.0
python
distance_eqn(5, 10)(20)
2100.0